package ne.analyzer.ui;

import java.util.List;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import ne.analyzer.entities.Reference;

/**
 * 题录表格
 * 
 * @author sunny
 *
 */
public class DataTable extends Composite {
	private List<Reference> refs;
	private Table tableRef;
	private TableViewer tableViewer;
	private ReferenceContentProvider refCP;
	private ReferenceLableProvider refLP;
	private String[] columns;
	private int[] widths;
	private boolean dataLoaded = false;

	public DataTable(Composite parent, int style, DataTableInf inf) throws Exception {
		super(parent, style);
		this.setLayout(new FormLayout());
		FormData fd = new FormData();
		fd.left = new FormAttachment(0);
		fd.top = new FormAttachment(0);
		fd.bottom = new FormAttachment(100);
		fd.right = new FormAttachment(100);
		tableViewer = new TableViewer(this, SWT.FULL_SELECTION | SWT.CENTER);
		tableRef = tableViewer.getTable();
		refCP = new ReferenceContentProvider();
		refLP = new ReferenceLableProvider(inf);
		tableViewer.setContentProvider(refCP);
		tableViewer.setLabelProvider(refLP);
		tableRef.setHeaderVisible(true);
		tableRef.setLinesVisible(true);
		tableRef.setVisible(true);
		tableRef.setLayoutData(fd);
		columns = inf.getColumns();
		widths = inf.getWidths();
		if (columns.length != widths.length)
			throw new Exception("width defined is not equal columns count");
	}

	public Table getTable() {
		return tableRef;
	}

	public String[] getColumns() {
		return columns;
	}

	public void setReferences(List<Reference> refs) {
		this.refs = refs;
		refresh();
	}

	private void refresh() {
		tableRef.clearAll(); // 删除所有的内容
		if (!dataLoaded) {
			for (int i = 0; i < columns.length; i++) {
				TableColumn tableColumn = new TableColumn(tableRef, SWT.BORDER);
				tableColumn.setText(columns[i]);
				tableColumn.setResizable(true);
				tableColumn.setWidth(widths[i]);
			}
			dataLoaded = true;
		}
		tableViewer.setInput(refs);
	}

	static class ReferenceContentProvider implements IStructuredContentProvider {

		@SuppressWarnings("unchecked")
		@Override
		public Object[] getElements(Object inputElement) {
			List<Object> ref = (List<Object>) inputElement;
			Object[] ret = ref.toArray(new Object[ref.size()]);
			return ret;
		}

	}

	static class ReferenceLableProvider implements ITableLabelProvider {
		private DataTableInf inf;

		public ReferenceLableProvider(DataTableInf inf) {
			this.inf = inf;
		}

		@Override
		public void addListener(ILabelProviderListener listener) {
		}

		@Override
		public void dispose() {
		}

		@Override
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener listener) {
		}

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			return inf.getColumnText(element, columnIndex);
		}
	}
}
